ria = {
    addItemCommands : function(ar, commandsParameters) {
        var requiredParameters = ['removeAction','insertAction','editAction'];
        for(i in requiredParameters) {
            if(commandsParameters[requiredParameters[i]] == undefined) {
                throw "addItemCommands requires the commandParameter '" + requiredParameters[i];
            }
        }
        for(i in ar) {
            ar[i].addCommand( 'callRemove', function(spec, json) {
                JsonC.callSfAction(commandsParameters.removeAction, {id:spec.ds.id});
            })
            .addCommand( 'showFormError', function(spec, json) {
                console.log(spec);
                var str = '#' + spec.extraTplValues._formId + ' label[for="' + spec.extraTplValues._objRequestName + '[' + json.fieldName + ']"]';
                var element = $(str);
                if(element.length == 0) {
                    throw 'Could not find the element "' + str + '" to show the form error: "' + json.message;
                }
                element.append(
                    '<em class="error">' + json.message + '</em>'
                )
            })
            .addCommand( 'cancelInsert' , function(spec, json) {
                UtilC.closeDialog();
            })
            .addCommand( 'edit' , function(spec,json) {
                this.showOnDialog({view:'edit'});
            })
            .addCommand( 'cancelEdit' , function(spec, json) {
                UtilC.closeDialog();
            })
            .addCommand( 'showOnDialog' , function(spec, json) {
                UtilC.displayDialog({width:800});
                this.setElement($('#UtilC-dialog')).display(json.view);
            })
            .addCommand( 'submitInsert', function(spec, json) {
                $('#' + spec.extraTplValues._formId + ' .error').remove();
                JsonC.submitForSfAction(
                    commandsParameters.insertAction,
                    {},
                    spec.extraTplValues._formId
                );
            })
            .addCommand('submitEdit', function(spec, json) {
                $('#' + spec.extraTplValues._formId + ' .error').remove();
                JsonC.submitForSfAction(
                    commandsParameters.editAction,
                    {},
                    spec.extraTplValues._formId
                );
            })
            .addCommand( 'update', function(spec, json) {
                this.setDs(json.ds).display('view').getElement().hide().fadeIn();
                if(spec.list) {
                    spec.list.setItemDs(json.ds);
                    spec.list.scrollToId({id:json.ds.id});
                }
                UtilC.closeDialog();
            })
            .addCommand( 'markCheckboxes', function(spec,json) {
                var selector = '#' + spec.extraTplValues._formId + " :checkbox.checktrue";
                $(selector).attr("checked",true);
            });

        }

    },

    addListCommands : function(ar, commandsParameters) {
        for(i in ar) {
            ar[i].addCommand(
                'refresh',
                function(spec,json) {
                    spec.riaObject.setActualView('view');
                    spec.itemsDs = json.itemsDs;
                    this.display();
                }
            )
            .addCommand(
                'scrollToId',
                function(spec,json) {
                    $('#' + spec.ds.listId ).scrollTo($('#' + spec.itemId + json.id),1000);
                }
            )
            .addCommand('sort', function(spec, params) {
                spec.itemsDs.sort(function (a, b) {
                    var x = a[params.field].toLowerCase();
                    var y = b[params.field].toLowerCase();
                    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
                });
                if(spec.sortOrder) {
                   if(spec.sortOrder == 'ASC') {
                       spec.itemsDs.reverse();
                       spec.sortOrder = 'DESC';
                   }
                   else {
                       spec.sortOrder = 'ASC';
                   }
                }
                else {
                    spec.sortOrder = 'ASC';
                }
                this.getRiaObject().setActualView('view');
                this.display();
            });
        }
    }
};


